package com.vodafone.nowplus.android.peoplemail.filter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.text.TextUtils;
import android.util.Log;

public class FilterManagerImpl360 implements FilterManager {

	
	
	private static final String FILTER_NAME_CUSTOM = "custom";
	private List<MessageListFilter> preconfiguredFilters = new ArrayList<MessageListFilter>();
	private List<MessageListFilter> filters = new ArrayList<MessageListFilter>();
	
	private final static MessageListFilter FILTER_ALL = new MessageListFilter("(?i:.*Inbox)", "all", "filter_icon_all");
	private final static MessageListFilter FILTER_360 = new MessageListFilter("(?i:Inbox)", "360", "filter_icon_360");
	
	public FilterManagerImpl360() {
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@gmail.com/Inbox)", "gmail", "filter_icon_google"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@googlemail.com/Inbox)", "google", "filter_icon_google"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@yahoo.com/Inbox)", "yahoo.com", "filter_icon_other"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@yahoo.de/Inbox)", "yahoo.de", "filter_icon_other"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@gmx.de/Inbox)", "gmx.de", "filter_icon_other"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@live.com/Inbox)", "live.com", "filter_icon_winlive"));
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@hotmail.com/Inbox)", "hotmail.com", "filter_icon_winlive"));
		// TODO add preconfigured filters for other mail providers here
		preconfiguredFilters.add(new MessageListFilter("(?i:.*@.*/Inbox)", FILTER_NAME_CUSTOM, "filter_icon_custom"));
		clearFilters();
    }
	
	@Override
	public void clearFilters() {
		Log.v("messageList", "clearing filters, adding ALL filter");
		filters.clear();
		filters.add(FILTER_ALL);
		filters.add(FILTER_360);
	}

	@Override
	public List<MessageListFilter> getFilters() {
		Collections.sort(filters);
		return filters;
	}

	@Override
	public boolean matchesFilter(MessageListFilter filter, String folderName) {
		if(filter==null) {
			// unknown filter
			return false;
		}
		return filter.matches(folderName);
	}

	@Override
	public boolean matchesFilter(String filterName, String folderName) {
		return matchesFilter(getFilterByName(filterName), folderName);
	}

	@Override
	public void notifyOfExistingFolder(String folderName) {
		Log.v("messageList", "FilterManager notified of folderName " + folderName);
		// is there a filter for this (except all)
		for(MessageListFilter f : filters) {
			if(folderNameToFilterName(folderName, f).equals(f.getName())) {
				// we have this already, so do nothing
				Log.v("messageList","we have this already: " + f);
				return;
			}
		}		
		Log.v("messageList","not yet in filter list");
		// look for preconfigured filters which match folderName and add them to actual filter list once
		for(MessageListFilter f : preconfiguredFilters) {
			if(f.matches(folderName)) {
				// create a new filter based on this folderName if not yet existing with the same name
				String filterName = folderNameToFilterName(folderName, f);
				MessageListFilter newFilter = new MessageListFilter(filterName.trim(), FILTER_NAME_CUSTOM, f.getIconId());
				// set name derived from this folderName (use email address)
				newFilter.setName(filterName.trim());

				Log.v("messageList", "adding new filter " + newFilter + " fitting for preconfiguredFilter " + f);
				// construct a regex matching for this particular folder
				String matchRegex = "(?i:"+folderName+")";
				newFilter.setMatchRegex(matchRegex );
				filters.add(newFilter);
				// only add the first match.
				return;
			}
		}

	}

	private String folderNameToFilterName(String folderName, MessageListFilter f) {
	    String filterName = TextUtils.split(folderName, "/")[0];
	    // is this not an aggregated account's inbox?
	    if(!filterName.contains("@")) {
	    	// no it isn't, so use the preconfigured filter's name.
	    	filterName = f.getName();
	    }
	    return filterName;
    }

//	@Override
//    public MessageListFilter getFilterByType(String name) {
//		for(MessageListFilter f : filters) {
//			if(f.getType().equals(name)) {
//				return f;
//			}
//		}
//	    return null;
//    }

	@Override
    public MessageListFilter getFilterForFolderName(String folderName) {
		for(MessageListFilter f : filters) {
			if(f.matches(folderName) && !FILTER_ALL.equals(f)) {
				return f;
			}
		}
		return null;
    }

	@Override
    public MessageListFilter getFilterByName(String messageFilterName) {
		for(MessageListFilter f : filters) {
			if(f.getName().equals(messageFilterName)) {
				return f;
			}
		}
	    return null;
    }

}
